<?php

Class Template {

    private $registry;
    public $template_content;
    private $vars = array();
 
    function __construct($registry) {
    	$this->registry = $registry;
    
    }
    
    public function __set($index, $value)
    {
           $this->vars[$index] = $value;
    }
    
    
    function getTemplate($template_name, array $tags = array()){
        $path  = __SITE_PATH."/views/$template_name.php";
        $template_content = file_get_contents($path);
        $template_content = str_replace("{__SERVER}", __SERVER, $template_content);
        foreach($tags as $tag=>$value){
            $template_content = str_replace("{".$tag."}", $value, $template_content);
        }
        return $template_content;
    }

    //load left content or right content
    function sideContent($name, $tags){
        return $this->getTemplate($name, $tags);
    }

    function getindex(){
        $tags = array(
            "user"        => $this->getuser(),
            "header"      => $this->getheader(),
            "mmenu"       => $this->getmmenu(),
            "body"        => $this->getbodytext()
        );
        return $this->getTemplate("index", $tags);
    }

        function getheader(){
            return $this->getTemplate("header", array());
        }
        function getmmenu(){
            return $this->getTemplate("mmenu", array());
        }
        function getuser(){
            if(!isset($_SESSION['username'])){
                return $this->getTemplate("login", array());
            }else{
                return $this->getTemplate("logout", array("username" => $_SESSION['username']));
            }
        }
    function masterbody($content){
        $tags = array(
            "content"     => $content,
            "footer"      => $this->getTemplate("footer", array())
        );
        return $this->getTemplate("indexView", $tags);
    }

    //==================indexController
    function home($district, $province){
        $right = null;
        if($this->article($district, $province) == 0){
             $right =  $this->masterbody("Không tìm thấy kết quả phù hợp!");
        }else{
            $script = '<script type="text/javascript">
            var members = new Array();'."\n" ;
            foreach($this->article($district, $province) as $ar){
                $script.= 'members.push('.$ar.');'."\n";
            }
            $right = $this->getTemplate("listarticle", array("script" =>$script ));
        }
        $tags = array(
            "left"    => $this->sideContent("listmenu", array()),
            "right"   => $right
        );
        return $this->masterbody($this->getTemplate("content", $tags));
    }

    function article($district, $province){
        if($district != "" & $province != ""){
            $result = $this->registry->db->getManager("recordManager")->getRecordOfSearch($district, $province);
        }else{
            $result = $this->registry->db->getManager("recordManager")->getRecordList();
        }
        $listArticle= array();
//        echo $result;
        if($result == 0){
            return 0;
        }else{

            while($record = mysql_fetch_assoc($result)){
                $tags = array(
                    "rid"               => $record['rid'],
                    "title"             => $record['title'],
                    "price"             => $record['price'],
                    "area"              => $record['area'],
                    "address"           => $record['address'],
                    "district"               => $record['district'],
                    "province"               => $record['province'],
                    "post_date"         => date("d-m-Y",strtotime($record['post_date'])),
                );
//            array_push($listArticle, "'".$this->registry->template->getTemplate("article", $tags)."'");
                $listArticle [] = '"'.str_replace("\n"," ",trim($this->registry->template->getTemplate("article", $tags))).'"';
             }
            return ($listArticle);
        }


    }


    function lienhe(){
        return $this->masterbody($this->getTemplate("contact", array()));
    }

    function gioithieu(){
        return $this->masterbody($this->getTemplate("gioithieu", array()));
    }

    function huongdan(){
        return $this->masterbody("Sorry! This site is under construction. Please come back later!");
    }

    function search(){

    }
    //==================mapController
    function map(){
        return $this->masterbody($this->getTemplate("radius", array()));
    }

//==================memberController

    //load only right content of memberController
    function mastermember($rightContent){
        $tags = array(
            "left"    => $this->sideContent("ppmenu", array()),
            "right"   => $rightContent
        );
        return $this->masterbody($this->getTemplate("content", $tags));
    }

    function member($uid){
        $re = $this->registry->db->getManager('userManager')->getinfo($uid);
        $tags = array(
            "fullname"          => $re['fullname'],
            "dob"               => date('d-m-Y', strtotime($re['dob'])),
            "address"           => $re['address'],
            "mobile"            => $re['mobile'],
            "sex"               => $re['sex'],
            "job"               => $re['job'],
            "email"             => $re['email'],
        );
        return $this->mastermember($this->sideContent("privatepage", $tags));
    }

    function doimatkhau(){
        return $this->mastermember($this->sideContent("changepass", array()));
    }

    function tindadang($uid){
        $result = $this->registry->db->getManager("recordManager")->getRecordOfUser($uid);
        $tindadang="";
        while($record = mysql_fetch_assoc($result)){
            $tags = array(
                "rid"                 => $record['rid'],
                "title"               => $record['title'],
            );
            $tindadang .= $this->registry->template->getTemplate("tindadang_sub", $tags);
        }
//        return $tindadang;
        return $this->mastermember($tindadang, array());
    }

//==================recordController
    function dangtin(){
        $tags = array(
            "left"    => isset($_SESSION['uid'])?"":$this->sideContent("left_dangtin", array()),
            "right"   => $this->getTemplate("dangtin_right", array())
        );
        return $this->masterbody($this->getTemplate("content", $tags));
    }

    function dangtin_map($uid, $rid, $address){
        $tags = array(
            "uid"   => $uid,
            "rid"=>$rid,
            "address" => $address!=""?$address:"Hồ Chí Minh"
        );
        return $this->masterbody($this->getTemplate("dangtin_map",$tags ));
    }

    public function detail($rid){
        if($r = $this->registry->db->getManager('recordManager')->detail($rid)){
            $tags_right = array(
                "title"     =>$r['title'],
                "description"   =>$r['description'],
                "area"          =>$r['area'],
                "price"         =>$r['price'],
                "address"       =>$r['address'],
                "district"      =>$r['district'],
                "province"      =>$r['province'],
                "lat"           =>$r['lat'],
                "lng"           =>$r['lng'],
                "image"         =>$r['image'],
            );
            $right = $this->registry->template->getTemplate("detail_right", $tags_right);
            
             $tags_left = array(
                "post_date"     =>date('d-m-Y',strtotime($r['post_date'])),
                "contact_name"  =>$r['contact_name'],
                "contact_mobile"=>$r['contact_mobile'],
                 "username"     =>$r['username']
                //"email"         =>$r['email'],
            );
            $left  = $this->registry->template->getTemplate("detail_left", $tags_left);
            
            $tags = array(
                "left"  =>$left,
                "right" =>$right
            );
            return $this->masterbody($this->getTemplate("content", $tags));
        }else{
            //neu ko ton tai rid
        }
        
        
    }

    function edit($rid){
        $r = $this->registry->db->getManager("recordManager")->getRecord($rid);
        if($r == 0){
            return $this->masterbody("Không tìm thấy tin này!");
        }else{
            foreach($r as $key => $value){
                $tags[$key] = $value;
            }
            $right = $this->getTemplate("editRecord", $tags);
            return $this->masterbody($this->getTemplate("content",array("left"    => "", "right"   => $right) ));
        }
    }

}

?>
